package org.findbugs.report;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.findbugs.report.entity.BugCollection;
import org.findbugs.report.entity.exception.ReportException;
import org.findbugs.report.view.PDFReporter;

public class FindBugsReporter {

    private Document findbugsXmlDocument;

    public FindBugsReporter(InputStream findbugsReport) throws ReportException {
        SAXReader reader = new SAXReader();
        try {
            findbugsXmlDocument = reader.read(findbugsReport);
        } catch (DocumentException e) {
            e.printStackTrace();
            throw new ReportException("Given XML document is not valid, is it XML?", e);
        }
    }

    public ByteArrayOutputStream getReport() throws ReportException, com.lowagie.text.DocumentException, IOException {
        BugCollection bugCollection = BugCollection.getBugCollection(findbugsXmlDocument);
        PDFReporter reporter = new PDFReporter(bugCollection);
        return reporter.getReport();
    }
}
